Imports System.Windows.Forms
Imports Vintasoft.Imaging
Imports Vintasoft.Imaging.Codecs
Imports Vintasoft.Imaging.ImageRendering

Namespace Imaging
	Public Partial Class ImageViewerSettingsForm
		Inherits Form

		#Region "Fields"

		Private _viewer As ImageViewer

		Private _renderingSettings As RenderingSettings

		Private _renderingRequirements As ImageRenderingRequirements

		#End Region



		#Region "Constructors"

		Public Sub New()
			InitializeComponent()

			Dim aStyles As Object() = New Object() {AnchorStyles.None, AnchorStyles.Left Or AnchorStyles.Top, AnchorStyles.Top, AnchorStyles.Top Or AnchorStyles.Right, AnchorStyles.Right, AnchorStyles.Bottom Or AnchorStyles.Right, _
				AnchorStyles.Bottom, AnchorStyles.Bottom Or AnchorStyles.Left, AnchorStyles.Left}

			imageAnchorComboBox.Items.AddRange(aStyles)
			focusPointAnchorComboBox.Items.AddRange(aStyles)

			renderingQualityComboBox.Items.Add(ImageRenderingQuality.Low)
			renderingQualityComboBox.Items.Add(ImageRenderingQuality.Normal)
			renderingQualityComboBox.Items.Add(ImageRenderingQuality.High)
		End Sub

		Public Sub New(viewer As ImageViewer)
			Me.New()
			_viewer = viewer
			ShowSettings()
		End Sub

		#End Region



		#Region "Methods"

		Private Sub ShowSettings()
			imageAnchorComboBox.SelectedItem = _viewer.ImageAnchor
			renderingQualityComboBox.SelectedItem = _viewer.RenderingQuality
			focusPointAnchorComboBox.SelectedItem = _viewer.FocusPointAnchor
			focusPointIsFixedCheckBox.Checked = _viewer.IsFocusPointFixed
			rendererCacheSizeNumericUpDown.Value = CInt(Math.Truncate(Math.Round(_viewer.RendererCacheSize)))
			viewerBufferSizeNumericUpDown.Value = CInt(Math.Truncate(Math.Round(_viewer.ViewerBufferSize)))
			minImageSizeWhenZoomBufferUsedNumericUpDown.Value = CInt(Math.Truncate(Math.Round(_viewer.MinImageSizeWhenZoomBufferUsed)))
			_renderingSettings = _viewer.RenderingSettings
			backgoundColorPanel.BackColor = _viewer.BackColor
		End Sub

		Private Sub SetSettings()
			_viewer.ImageAnchor = CType(imageAnchorComboBox.SelectedItem, AnchorStyles)
			_viewer.RenderingQuality = DirectCast(renderingQualityComboBox.SelectedItem, ImageRenderingQuality)
			_viewer.FocusPointAnchor = CType(focusPointAnchorComboBox.SelectedItem, AnchorStyles)
			_viewer.IsFocusPointFixed = focusPointIsFixedCheckBox.Checked
			_viewer.RendererCacheSize = CInt(Math.Truncate(rendererCacheSizeNumericUpDown.Value))
			_viewer.ViewerBufferSize = CInt(Math.Truncate(viewerBufferSizeNumericUpDown.Value))
			_viewer.MinImageSizeWhenZoomBufferUsed = CInt(Math.Truncate(minImageSizeWhenZoomBufferUsedNumericUpDown.Value))

			_viewer.RenderingSettings.InterpolationMode = _renderingSettings.InterpolationMode
			_viewer.RenderingSettings.SmoothingMode = _renderingSettings.SmoothingMode
			_viewer.RenderingSettings.Resolution = _renderingSettings.Resolution

			_viewer.BackColor = backgoundColorPanel.BackColor
			If _renderingRequirements IsNot Nothing Then
				_viewer.RenderingRequirements = _renderingRequirements
			End If
		End Sub

		Private Sub renderingSettingsButton_Click(sender As Object, e As EventArgs)
			Dim renderingSettingsDialog As New RenderingSettingsForm(_renderingSettings)
			If renderingSettingsDialog.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
				_renderingSettings = renderingSettingsDialog.RenderingSettings
			End If
		End Sub

		Private Sub setBackgoundColorButton_Click(sender As Object, e As EventArgs)
			colorDialog1.Color = backgoundColorPanel.BackColor
			If colorDialog1.ShowDialog() = DialogResult.OK Then
				backgoundColorPanel.BackColor = colorDialog1.Color
			End If
		End Sub

		Private Sub buttonOk_Click(sender As Object, e As EventArgs)
			SetSettings()

			DialogResult = DialogResult.OK
		End Sub

		Private Sub buttonCancel_Click(sender As Object, e As EventArgs)
			DialogResult = DialogResult.Cancel
		End Sub


		Private Sub renderingRequirementsButton_Click(sender As Object, e As EventArgs)
			If _renderingRequirements Is Nothing Then
				_renderingRequirements = New ImageRenderingRequirements(_viewer.RenderingRequirements)
			End If
			Dim renderingRequirements As New ImageRenderingRequirementsForm(_renderingRequirements)
			If renderingRequirements.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
				_renderingRequirements = renderingRequirements.RenderingRequirements
			End If
		End Sub


		#End Region

	End Class
End Namespace
